package com.web.user.member.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.mysteel.util.ObjectUtils;

public class URLFilter implements Filter
{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException
	{
		
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException
	{
		
		HttpServletRequest req= (HttpServletRequest) request;   
		HttpServletResponse resp= (HttpServletResponse) response;   
		String urlPath = req.getServletPath ();
		String[] str = urlPath.split ("/");
		String memberId = "";
		if((ObjectUtils.notNull (str))&&str.length==3)
		{
			memberId = str[str.length-1];
		
		Cookie[] ck = req.getCookies ();
		
		if(ObjectUtils.notNull (ck))
		{
			for(Cookie cookie:ck)
			{
				if(cookie.getName ().equals ("member_id"))
				{
					if(memberId.equals (cookie.getValue ()))
					{
						request.setAttribute ("memberId" ,cookie.getValue ());
						request.setAttribute ("type" ,1); //��������Լ��ռ�
						request.getRequestDispatcher("/kj/kongjian.html?memberId="+cookie.getValue ()).forward(request, response);
						
						return;   
					}else
					{
						request.setAttribute ("memberId" ,memberId);
						request.setAttribute ("type" ,2); //������ʱ��˵Ŀռ�
						resp.sendRedirect ("/see/"+memberId);
						
						return;   
					}
				}
			}
		}
		 resp.sendRedirect ("/login?callback="+urlPath);
		
		 return ;
		}else
		{
			request.getRequestDispatcher("/basic/404.htm").forward(request, response);
			
			return ;
		}
	}

	@Override
	public void destroy()
	{
		
		
	}
	
}
